//
//  MSQRCodeView.m
//  MSMain
//
//  Created by Dgh Hv on 2018/9/19.
//  Copyright © 2018年 土豆. All rights reserved.
//

#import "MSQRCodeView.h"

@interface MSQRCodeView ()

@property (nonatomic, strong)UIImage * QRImage;

@end

@implementation MSQRCodeView

- (instancetype)initWithFrame:(CGRect)frame AndQRImage:(UIImage *)image{
    if (self = [super initWithFrame:frame]) {
        _QRImage = image;
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    UIView * backView = [[UIView alloc]init];
    backView.backgroundColor = KWhiteColor;
    ViewRadius(backView, 12);
    [self addSubview:backView];
    
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(270, 260));
    }];
    
    UILabel * title = [[UILabel alloc]init];
    title.text = @"扫描下载想爱";
    title.textColor = RGB_HEX(0x666666);
    title.font = SYSTEMFONT(15);
    [backView addSubview:title];
    
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backView.mas_centerX);
        make.top.equalTo(backView.mas_top).offset(15);
        make.height.equalTo(@21);
    }];
    
    UIButton * closeBtn = [[UIButton alloc]init];
    [closeBtn setImage:IMAGE_NAMED(@"common_but_chacha_n") forState:UIControlStateNormal];
    [backView addSubview:closeBtn];
    
    [closeBtn addTapBlock:^(UIButton *btn) {
        self.hidden = YES;
    }];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.top.equalTo(backView);
        make.size.mas_equalTo(CGSizeMake(34, 34));
    }];
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = RGB_HEX(0xe6e6e6);
    [backView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(backView);
        make.height.equalTo(@0.5);
        make.top.equalTo(title.mas_bottom).offset(13);
    }];
    
    UIImageView * QRImageView = [[UIImageView alloc]init];
    QRImageView.image = self.QRImage;
    [backView addSubview:QRImageView];
    
    [QRImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backView.mas_centerX);
        make.top.equalTo(lineView.mas_bottom).offset(28);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    UIButton * saveButton = [[UIButton alloc]init];
    [saveButton setTitle:@"保存到手机" forState:UIControlStateNormal];
    [saveButton setTitleColor:KWhiteColor forState:UIControlStateNormal];
    saveButton.titleLabel.font = SYSTEMFONT(16);
    saveButton.backgroundColor = RGB_HEX(0xFFBB01);
    ViewRadius(saveButton, 5);
    [saveButton addTapBlock:^(UIButton *btn) {
        UIImageWriteToSavedPhotosAlbum(self.QRImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }];
    [backView addSubview:saveButton];
    
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView.mas_left).offset(15);
        make.right.equalTo(backView.mas_right).offset(-15);
        make.height.equalTo(@40);
        make.top.equalTo(QRImageView.mas_bottom).offset(33);
    }];
}

// 指定回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    NSString *msg = nil ;
    if(error != NULL){
        msg = @"保存图片失败" ;
    }else{
        msg = @"保存图片成功" ;
    }
    [MBProgressHUD showContent:msg];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
